﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using _52fzc.Model.HttpModel;

namespace _52fzc.Model.PattenModel
{
    public class Context : BaseEntity
    {
        public static readonly string ErrorMessage = "ERROR_MESSAGE";

        protected bool _boolVal = false;
        protected string _strVal = "";
        protected int _intVal = 0;
        protected float _floatVal = 0;
        protected double _doubleVal = 0;
        protected RequestModel _request = null;
        protected ResponseModel _response = null;
        protected IDictionary<string, object> _contextParam = new Dictionary<string, object>();
        protected bool _isError = false;
        protected Exception _exception = null;
        protected SessionModel _session = null;

        public virtual bool BoolVal { get { return _boolVal; } set { _boolVal = value; } }
        public virtual string StrVal { get { return _strVal; } set { _strVal = value; } }
        public virtual int IntVal { get { return _intVal; } set { _intVal = value; } }
        public virtual float FloatVal { get { return _floatVal; } set { _floatVal = value; } }
        public virtual double DoubleVal { get { return _doubleVal; } set { _doubleVal = value; } }

        public Context(RequestModel request, ResponseModel response)
        {
            _request = request;
            _response = response;
            string[] requestKeys = _request.GetKeys();
            foreach (string key in requestKeys)
            {
                Put(key.ToLower(), _request.Get(key));
            }
        }

        public virtual RequestModel Request
        {
            get
            {
                return _request;
            }
        }

        public virtual ResponseModel Response
        {
            get
            {
                return _response;
            }
        }

        public virtual object Get(string paramName, object defaultVal)
        {
            if (_contextParam.ContainsKey(paramName.ToLower()))
            {
                return _contextParam[paramName.ToLower()];
            }
            else
            {
                return defaultVal;
            }
        }

        public virtual object Get(string paramName)
        {
            return Get(paramName, null);
        }

        public virtual string GetString(string paramName, string defaultVal)
        {
            return (Get(paramName, defaultVal)).ToString();
        }

        public virtual string GetString(string paramName)
        {
            return GetString(paramName, "");
        }

        public virtual int GetInt(string paramName, int defaultVal)
        {
            return Convert.ToInt32(Get(paramName, defaultVal));
        }

        public virtual int GetInt(string paramName)
        {
            return GetInt(paramName, 0);
        }

        public virtual float GetFloat(string paramName, float defaultVal)
        {
            return (float)Get(paramName, defaultVal);
        }

        public virtual float GetFloat(string paramName)
        {
            return GetFloat(paramName, 0);
        }

        public virtual double GetDouble(string paramName, double defaultVal)
        {
            return Convert.ToDouble(Get(paramName, defaultVal));
        }

        public virtual double GetDouble(string paramName)
        {
            return GetDouble(paramName, 0);
        }

        public virtual void Put(string paramName, object paramVal)
        {
            if (_contextParam.ContainsKey(paramName.ToLower()))
            {
                _contextParam[paramName.ToLower()] = paramVal;
            }
            else
            {
                _contextParam.Add(paramName.ToLower(), paramVal);
            }
        }

        public virtual Exception Exception
        {
            get
            {
                return _exception;
            }

            set
            {
                Put(Context.ErrorMessage, _exception.Message);
                _exception = value;
            }
        }

        public virtual bool IsError
        {
            get
            {
                if (_exception == null) return false;
                return true;
            }
        }

        public virtual SessionModel Session
        {
            get
            {
                return _session;
            }
            set
            {
                _session = value;
            }
        }

        public virtual bool GetBoolean(string paramName, bool defaultVal)
        {
            return (bool)(Get(paramName, defaultVal));
        }

        public virtual bool GetBoolean(string paramName)
        {
            return (bool)(Get(paramName, false));
        }
    }
}
